package com.jack.helloworld.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jack.helloworld.R;

public class ProgressBarActivity extends AppCompatActivity implements View.OnClickListener {

    ProgressBar progressBarH;
    TextView tvProgressBar;
    Button btnAdd;
    Button btnReduce;
    Button btnReset;
    Button btnShowDialog;

    int firstProgress;
    int secondProgress;
    int max;

    String firstPercent;
    String secondPercent;

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_progress_bar);

        initView();
        initData();

    }

    private void initView() {
        progressBarH = (ProgressBar) findViewById(R.id.progressBarH);
        tvProgressBar = (TextView) findViewById(R.id.tvProgressBar);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnReduce = (Button) findViewById(R.id.btnReduce);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnShowDialog = (Button) findViewById(R.id.btnShowDialog);

    }

    private void initData() {
        btnAdd.setOnClickListener(this);
        btnReduce.setOnClickListener(this);
        btnReset.setOnClickListener(this);
        btnShowDialog.setOnClickListener(this);

        firstProgress = progressBarH.getProgress();
        secondProgress = progressBarH.getSecondaryProgress();
        max = progressBarH.getMax();

        getPercent();
    }

    private void getPercent() {
        firstPercent = (int)((firstProgress / (float)max) * 100) + "%";
        secondPercent = (int)((secondProgress / (float)max) * 100) + "%";
        tvProgressBar.setText("第一进度条:" + firstPercent + ",第二进度条:" + secondPercent);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btnAdd:
                firstProgress += 10;
                secondProgress += 10;
                progressBarH.incrementProgressBy(10);
                progressBarH.incrementSecondaryProgressBy(10);
                break;
            case R.id.btnReduce:
                firstProgress -= 10;
                secondProgress -= 10;
                progressBarH.incrementProgressBy(-10);
                progressBarH.incrementSecondaryProgressBy(-10);
                break;
            case R.id.btnReset:
                firstProgress = 50;
                secondProgress = 20;
                progressBarH.setProgress(firstProgress);
                progressBarH.setSecondaryProgress(secondProgress);
                break;
            case R.id.btnShowDialog:
                showProgressDialog();
                break;
        }

        getPercent();
    }

    private void showProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("download");
        progressDialog.setMessage("download message");
        progressDialog.setIcon(R.mipmap.ic_launcher);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.incrementProgressBy(50);
        progressDialog.setIndeterminate(false);

        progressDialog.setButton(DialogInterface.BUTTON_POSITIVE ,"ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(ProgressBarActivity.this, "download finish", Toast.LENGTH_SHORT).show();
            }
        });

        progressDialog.setCancelable(true);

        progressDialog.show();
    }
}
